package com.cindata.dictionary

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['isAuthenticated()'])
@Transactional
class CollateralStageController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index() 
    {

        def max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        def offset = params.offset ? params.offset.toInteger() : 0

        def code = params['code']
        def name = params['name']

        def sql = "from CollateralStage where 1=1 "
        if (code) 
        {
            sql += " and code like '%${code}%'"    
        }
        if (name) 
        {
            sql += " and name like '%${name}%'"
        }

        def countSql = "select count(id) " + sql
        sql += " order by name desc "

        def collateralStageList = CollateralStage.executeQuery(sql, [max: max, offset: offset])
        def collateralStageCount = CollateralStage.executeQuery(countSql)
        respond collateralStageList, model: [collateralStageCount: collateralStageCount[0], params: params], view: 'index'
    }

    def show(CollateralStage collateralStage) {
        respond collateralStage
    }

    def create() {
        respond new CollateralStage(params)
    }

    @Transactional
    def save(CollateralStage collateralStage) {
        if (collateralStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (collateralStage.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond collateralStage.errors, view:'create'
            return
        }

        collateralStage.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'collateralStage.label', default: 'CollateralStage'), collateralStage.id])
                redirect collateralStage
            }
            '*' { respond collateralStage, [status: CREATED] }
        }
    }

    def edit(CollateralStage collateralStage) {
        respond collateralStage
    }

    @Transactional
    def update(CollateralStage collateralStage) {
        if (collateralStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (collateralStage.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond collateralStage.errors, view:'edit'
            return
        }

        collateralStage.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'collateralStage.label', default: 'CollateralStage'), collateralStage.id])
                redirect collateralStage
            }
            '*'{ respond collateralStage, [status: OK] }
        }
    }

    @Transactional
    def delete(CollateralStage collateralStage) {

        if (collateralStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        collateralStage.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'collateralStage.label', default: 'CollateralStage'), collateralStage.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'collateralStage.label', default: 'CollateralStage'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
